#-*- coding: utf-8 -*-
from django.shortcuts import render
from django.conf import settings 
from shoplist.lists_gen import list_production, list_shopping, list_general, select_dict_by_name
from shoplist.forms import shoplist_form
from django.http import HttpResponse
from django.template.response import TemplateResponse
from eve_static.models import Invtypes

def shoplist(request):
    djangoRoot = settings.DJANGO_ROOT
    projectPath = settings.PROJECT_ROOT
    sitePath = settings.SITE_ROOT
    
    staticDir1 = settings.STATICFILES_DIRS[0]
#     staticDir2 = settings.STATICFILES_DIRS[1]
    form=shoplist_form()
    try:
        list_mat = request.session['list_mat']
    except:
        list_mat = []
    try:
        list_minimum = request.session['list_minimum']
    except:
        list_minimum = []
    if request.method == 'POST':
        form = shoplist_form(request.POST)
        if form.is_valid():
            typename = form.cleaned_data['typename']
            quantity = form.cleaned_data['quantity']
            invention = form.cleaned_data['invention']
            dict_mat = select_dict_by_name(list_mat, typename)
            if dict_mat != {}:
                if quantity == 0 or dict_mat['qte']+quantity <= 0:
                    list_mat.remove(dict_mat)
                else:
                    dict_mat['qte'] += quantity
                    dict_mat['new'] = 1
            else:
                list_mat += [{'name': typename, 'qte':quantity, 'inv':invention, 'new':1}]


    if list_mat != []:
        list_prod = []
        list_shop = []
        see = 1
        list_mat = list_general(list_mat, list_minimum)
        list_production(list_mat, list_minimum, {'lvl_sub_mat':0, 'pi':'one go', 'qle': 11}, list_prod)
        list_shopping(list_mat, list_minimum, {'lvl_sub_mat':0}, list_shop)
        
    else:
        see = 0
        
    request.session['list_mat'] = list_mat
    request.session['list_minimum'] = list_minimum
    
    return render(request, 'shoplist.html', locals())

def update_shop(request, typeid, action):
    list_shop = []
    try:
        list_mat = request.session['list_mat']
    except:
        list_mat = []
    try:
        list_minimum = request.session['list_minimum']
    except:
        list_minimum = []
    if action == 'A':
        list_minimum.append(int(typeid))
    else:
        list_minimum.remove(int(typeid))
    request.session['list_minimum'] = list_minimum
    list_shopping(list_mat, list_minimum, {'lvl_sub_mat':0}, list_shop)
    request.session['list_shop'] = list_shop
    return TemplateResponse(request, 'list_plus.html', {'l': list_shop, 'param':'name'})

def update_prod(request):
    list_prod = []
    try:
        list_mat = request.session['list_mat']
    except:
        list_mat = []
    try:
        list_minimum = request.session['list_minimum']
    except:
        list_minimum = []
    list_production(list_mat, list_minimum, {'lvl_sub_mat':0, 'pi':'one go', 'qle': 6}, list_prod)
    return TemplateResponse(request, 'list_prod.html', {'l':list_prod, 'param':'stage'})

def reset(request):
    try:
        del request.session['list_mat']
    except:
        pass
    try:
        del request.session['list_minimum']
    except:
        pass
    #si on clear la session on est logout :(
    #request.session.clear()
    return render(request, 'shoplist.html', locals())





def valkyrie(request):
    var = 15510
    return render(request, 'valkyrie.html')

# module de test, à del plus tard
def test(request):
    try:
        list_shop = request.session['list_shop']
    except:
        list_shop = []
    return render(request, 'list_prod.html', {'l': list_shop, 'param':'name'})

def test2(request):
    try:
        list_minimum = request.session['list_minimum']
    except:
        list_minimum = []
    c = 1
    return TemplateResponse(request, 'test.html', locals())
